import java.util.HashMap;
import java.util.Map;

class Solution {
    public int minNumberOfFrogs(String croakOfFrogs) {
        char[] chs = croakOfFrogs.toCharArray();
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n];

        Map<Character, Integer> index = new HashMap<>(); // 字符，字符对应的下标
        for(int i = 0; i < n; i++) {
            index.put(t.charAt(i), i);
        }

        for(char ch : chs) {
            if(ch == t.charAt(0)) {
                // 如果有青蛙刚结束一轮，让它复用
                if(hash[n-1] != 0) hash[n-1]--;
                hash[0]++;
            } else {
                int i = index.get((ch));
                if(hash[i-1] == 0) return -1;
                hash[i-1]--;
                hash[i]++;
            }
        }

        for(int i = 0; i < n-1; i++) {
            // 检查是否所有阶段都已归零
            if(hash[i] != 0) return -1;
        }

        // 返回所需青蛙的最大值
        return hash[n-1];
    }
}